package com.yylending.las.authority.password;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import butterknife.*;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.main.MainActivity;

/**
 * Created by Pengrf on 2016/11/2 16:31.
 *
 * @更新者 $Author$ 重置密码界面
 * @更新时间 2016/11/2
 */
public class ResetPasswordFragment extends BaseFragment implements ResetPasswordPresenter.View {
	private final static String TAG = "ResetPasswordFragment";

	private final static int LOADER_ID = 202;
	private final static int sectionNumber = 2;
	private final static String BILL_ID = "bill_id";

	@BindView(R.id.reset_password)
	EditText mNewPassword;
	@BindView(R.id.reset_password_delete)
	ImageView mPasswordDelete;
	@BindView(R.id.reset_confirm_new_password)
	EditText mConfirmPassword;
	@BindView(R.id.reset_confirm_new_password_delete)
	ImageView mConfirmPwdDelete;
	Unbinder unbinder;
	@BindView(R.id.reset_eyes_open)
	CheckBox mPasswordVisible;
	@BindView(R.id.reset_eyes_open2)
	CheckBox mConfirmPwdVisible;

	private ResetPasswordPresenter mPresenter;

	private String mBillId;

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(getActivity(), new ResetPasswordPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (ResetPasswordPresenter) data;
		mPresenter.attach(this);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		getLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public void onDetach() {
		CLog.i(TAG, "onDetach()");
		getLoaderManager().destroyLoader(LOADER_ID);
		super.onDetach();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	public static BaseFragment newInstance(String billId) {
		BaseFragment fragment = new ResetPasswordFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		args.putString(BILL_ID, billId);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null)
			mBillId = bundle.getString(BILL_ID);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_reset_password, null);
		unbinder = ButterKnife.bind(this, v);
		return v;
	}

	/**
	 * 输入密码时监听
	 */
	@OnTextChanged(value = R.id.reset_password, callback = OnTextChanged.Callback.TEXT_CHANGED)
	public void onPasswordChanged(CharSequence s, int start, int before, int count) {
		if (s.toString().trim().length() != 0) {
			mPasswordDelete.setVisibility(View.VISIBLE);
		} else {
			mPasswordDelete.setVisibility(View.GONE);
		}
	}

	/**
	 * 输入确认密码时监听
	 */
	@OnTextChanged(value = R.id.reset_confirm_new_password, callback = OnTextChanged.Callback.TEXT_CHANGED)
	public void onConfirmPwdChanged(CharSequence s, int start, int before, int count) {
		if (s.toString().trim().length() != 0) {
			mConfirmPwdDelete.setVisibility(View.VISIBLE);
		} else {
			mConfirmPwdDelete.setVisibility(View.GONE);
		}
	}

	/**
	 * 密码可见CheckBox监听
	 */
	@OnCheckedChanged({R.id.reset_eyes_open, R.id.reset_eyes_open2})
	public void onPasswordChecked(CompoundButton btn, boolean checked) {
		if(checked){
			mNewPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			mConfirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
		}else{
			mNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
			mConfirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}
		mNewPassword.setSelection(mNewPassword.getText().length());
		mConfirmPassword.setSelection(mConfirmPassword.getText().length());
	}

	@OnClick({R.id.reset_pwd_next_btn, R.id.reset_password_delete, R.id.reset_confirm_new_password_delete})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.reset_pwd_next_btn:
				YJKD.hideInputWindow(getActivity());
				String newPwd = mNewPassword.getText().toString().trim();
				String confirmPwd = mConfirmPassword.getText().toString().trim();
				if (newPwd.length() < 6 || confirmPwd.length() < 6) {
					showToast(R.string.password_invalid);
				} else if (!(newPwd.equals(confirmPwd))) {
					showToast(R.string.confirm_pwd_different);
				} else {
					//mPresenter.resetPassword(mBillId, confirmPwd);
				}
				break;
			case R.id.reset_password_delete:
				mNewPassword.getText().clear();
				break;
			case R.id.reset_confirm_new_password_delete:
				mConfirmPassword.getText().clear();
				break;
			default:
				break;
		}
	}

	@Override
	public void resetPwdSuccess() {
		showToast(R.string.pwd_reset_success);
		YJKD.logout();
		MainActivity.start(getActivity(), true);
	}

	@Override
	public void resetPwdFail(String code, String msg) {
		showToast(msg);
	}

	@Override
	public void resetPwdRetCode(String retCode) {
		StatService.onEventWithSingleAttribute(getContext(), StatIDSet.EVENT_ID_FORGOT_PASSWORD,retCode);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
	}
}